package com.linx.park.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.linx.common.core.domain.R;
import com.linx.common.core.validate.AddGroup;
import com.linx.common.core.validate.EditGroup;
import com.linx.common.excel.utils.ExcelUtil;
import com.linx.common.idempotent.annotation.RepeatSubmit;
import com.linx.common.log.annotation.Log;
import com.linx.common.log.enums.BusinessType;
import com.linx.common.mybatis.core.page.PageQuery;
import com.linx.common.mybatis.core.page.TableDataInfo;
import com.linx.common.web.core.BaseController;
import com.linx.common.web.utils.GisTokenUtils;
import com.linx.park.domain.bo.ParkPlaceBo;
import com.linx.park.domain.vo.*;
import com.linx.park.service.IParkPlaceService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 园区区域
 *
 * @author 泽智科技
 * @date 2024-10-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/park/park-place")
public class ParkPlaceController extends BaseController {

    private final IParkPlaceService parkPlaceService;

    /**
     * 查询园区区域列表
     */
    @SaCheckPermission("park:park-place:list")
    @GetMapping("/list")
    public TableDataInfo<ParkPlaceVo> list(ParkPlaceBo bo, PageQuery pageQuery) {
        return parkPlaceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出园区区域列表
     */
    @SaCheckPermission("park:park-place:export")
    @Log(title = "园区区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ParkPlaceBo bo, HttpServletResponse response) {
        List<ParkPlaceVo> list = parkPlaceService.queryList(bo);
        ExcelUtil.exportExcel(list, "园区区域", ParkPlaceVo.class, response);
    }

    /**
     * 获取园区区域详细信息
     *
     * @param parkPlaceId 主键
     */
    @SaCheckPermission("park:park-place:query")
    @GetMapping("/{parkPlaceId}")
    public R<ParkPlaceVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long parkPlaceId) {
        return R.ok(parkPlaceService.queryById(parkPlaceId));
    }

    /**
     * 新增园区区域
     */
    @SaCheckPermission("park:park-place:add")
    @Log(title = "园区区域", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ParkPlaceBo bo) {
        return toAjax(parkPlaceService.insertByBo(bo));
    }

    /**
     * 修改园区区域
     */
    @SaCheckPermission("park:park-place:edit")
    @Log(title = "园区区域", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ParkPlaceBo bo) {
        return toAjax(parkPlaceService.updateByBo(bo));
    }

    /**
     * 删除园区区域
     *
     * @param parkPlaceIds 主键串
     */
    @SaCheckPermission("park:park-place:remove")
    @Log(title = "园区区域", businessType = BusinessType.DELETE)
    @DeleteMapping("/{parkPlaceIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] parkPlaceIds) {
        return toAjax(parkPlaceService.deleteWithValidByIds(List.of(parkPlaceIds), true));
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "园区区域", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        List<ParkPlaceImportVo> parkPlaceImportVos = ExcelUtil.importExcel(file.getInputStream(),
            ParkPlaceImportVo.class);
        parkPlaceService.importData(parkPlaceImportVos);
        System.out.println("导入");
        return R.ok();
    }


    /**
     * 园区区域树形结构
     */
    @GetMapping("/parkTree")
    public R<List<Tree<Long>>> parkTree(ParkPlaceBo bo) {
        return R.ok(parkPlaceService.selectParkTreeList(bo));
    }

    /**
     * 园区区域树形结构带园区
     */
    @GetMapping("/parkTreeList")
    public R<List<Tree<Long>>> parkTreeList(ParkPlaceBo bo) {
        return R.ok(parkPlaceService.parkTreeList(bo));
    }

    /**
     * @Description:同步数据
     * @Param: * @param parkPlaceId
     * @return: com.linx.common.core.domain.R<com.linx.park.domain.vo.ParkPlaceVo>
     * @Author: gushaoliang
     * @Date: 2024/11/23 上午9:31
     */
    @SaCheckPermission("park:park-place:syncDate")
    @GetMapping("/syncDate")
    public R<Void> syncDate() {
        return toAjax(parkPlaceService.syncDate());
    }

    /**
     * 获取园区空间信息导入模板
     */
    @PostMapping("/importPlaceTemplate")
    public void importPlaceTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "园区空间信息", ParkPlaceExcelTempVo.class, response);
    }

    /**
     * 获取园区点位信息导入模板
     */
    @PostMapping("/importPointTemplate")
    public void importPointTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "园区点位间信息", ParkPlacePointVo.class, response);
    }

    /**
     * 园区点位信息导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "园区区域", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importPointData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importPointData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        List<ParkPlacePointImportVo> pointImportVos = ExcelUtil.importExcel(file.getInputStream(),
            ParkPlacePointImportVo.class);
        parkPlaceService.importPointData(pointImportVos);
        return R.ok();
    }

    /**
     * 根据园区区域查询园区点位信息
     */
    @GetMapping("/selectParkPlacePointByPlaceId/{parkPlaceId}")
    public R<List<ParkPlacePointVo>> selectParkPlacePointByPlaceId(@PathVariable Long parkPlaceId) {
        return R.ok(parkPlaceService.selectParkPlacePointByPlaceId(parkPlaceId));
    }


    /**
     * 获取GIS统一平台认证
     */
    @GetMapping("/getToken")
    public R<String> getToken() {
        String gisToken = new GisTokenUtils().getGisToken();
        return R.ok("操作成功", gisToken);
    }

}
